const app = getApp();
var util = require('../../utils/util.js'); //工具类
const http = require('../../service/http.js');//请求类
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //---------初始化信息---------------
    pageUrl: '', //跳转页面
    userInfo: '', //用户信息
    personOrderInfo: '', //订单信息
    pesonalCartInfo: '', //购物车数量
    customerServicePhone: '', //客服电话
    couponCount:'',//优惠券数量
    isBindPhone: '', //是否已经绑定手机
    //------用户绑定手机操作-------------
    animationData: '', //遮罩层动画
    // opMask:0,
    isshow: false,
    showBindPhoneModel: false, //绑定手机模态框
    isPhone: false,
    phoneNum:'',//需要绑定的手机号

    //-----登录--------------------------
    hasShop: false,//自己是否已经拥有分销店铺
    isOwnShop: '', //是否为自己的店铺：1---自己的店铺；0--别人的店铺；null---平台
    isVip: '', //是否是vip
  },
  onLoad: function() {
    wx.showTabBar();
    this.getShopInfo(); //获取登录授权情况、店铺信息
    console.log('onLoad', wx.getStorageSync('vip'));;
  },

  getShopInfo: function() { //获取登录授权情况
    let shopInfo = wx.getStorageSync('shopInfo');
    let isOwnShop = shopInfo.isOwnShop; //是否是自己的店铺
    this.setData({
      isAuthorization: app.data.isAuthorization,
      userInfo: wx.getStorageSync('userInfo'),
      isBindPhone: wx.getStorageSync('isBindPhone'), //检查用户是否绑定手机
      isOwnShop: isOwnShop || '', //用户是否拥有店铺
      isVip: wx.getStorageSync('vip')
    })
  },
  onShow:function(){
    if (app.data.shopInfoTimeOut === true) {//非分享且已经过期，清除店铺信息刷新店铺信息
      app.checkSession();
    }
    this.getElementsCounts();//获取订单数量
  },
  getElementsCounts: function() {
    
    let isAuthorization = this.data.isAuthorization;
    console.log('userjs:isAuthorization 授权情况' + isAuthorization);
    if (isAuthorization === true) { //已授权
      util.showLoading('加载中', (e)=> {
        app.getElementsCounts(); //获取订单、购物车数量
        let couponCountTask = setInterval(()=>{
          let couponCount  = app.data.couponCount;
          if (couponCount !== ''){
            clearInterval(couponCountTask);
            this.setData({
              personOrderInfo: app.data.personOrderInfo, //用户订单信息
              pesonalCartInfo: app.data.pesonalCartInfo, //用户购物车信息
              customerServicePhone: app.data.customerServicePhone, //客服电话
              couponCount: app.data.couponCount,//优惠券数量
            })
            wx.hideLoading();
          }
        },200)
      });
    }else{
      app.SessionTimeOut();
    }
  },
  toShopingCart: function(e) { //跳转我的购物车
    wx.switchTab({
      url: '/pages/shopingCart/shopingCart',
    })
  },
  checkOrder: function(e) {
    console.log('checkOrder', e);

    let listType = e.currentTarget.dataset.type;
    let activeIndex = 0; //tab位置
    let statusOri = ''; //订单状态
    if (listType == 'all') {
      activeIndex = 0;
    } else if (listType == 'unpay') {
      activeIndex = 1;
      statusOri = '10';
    } else if (listType == 'unGroup') {
      activeIndex = 2;
      statusOri = '11';
    } else if (listType == 'unshipped') {
      activeIndex = 3;
      statusOri = '20';
    } else if (listType == 'shipped') {
      activeIndex = 4;
      statusOri = '30';
    } else if (listType == 'complete') {
      activeIndex = 5;
      statusOri = '50';
    }
    console.log(activeIndex);
    wx.navigateTo({
      url: '/pages/user/pages/orders/orders?activeIndex=' + activeIndex + '&&statusOri=' + statusOri,
    })
  },
  //---------------用户绑定手机号弹出框-------------------------
  bindPhone: function(e) {
    console.log('用户绑定手机号',e);
    // wx.hideTabBar(); //隐藏底部菜单
    Http.getReq('/weixin/wxUser/getWxPhone', {
      encryptedData:e.detail.encryptedData,
      iv:e.detail.iv,
      rdSession: wx.getStorageSync('rd_session')
    },  (res)=> {
      console.log('bindPhone-', res);
      let [phoneNum, isPhone] = [res.data.purePhoneNumber,true]
 
      if(!res.ret){
        util.showToast(res.msg,1000,'none');
        isPhone = false
      }
      this.setData({
        showBindPhoneModel: true,
        isshow: true,
        phoneNum: phoneNum || '',//需要绑定的手机号
        isPhone: isPhone,
      })
    })
  },
  //保存手机号 
  savePhone: function(e) {
    console.log(e);
    this.closeModel(e);
    this.setData({
      isBindPhone: e.detail.isBindPhone,
    })
  },
  //关闭绑定模态框
  closeModel: function(e) {
    console.log(e);
    this.setData({
      showBindPhoneModel: e.detail.showBindPhoneModel,
      isshow: false
    });
    wx.showTabBar(); //显示底部菜单
  },
  //--------------
  backToHome: function(e) { //返回到首页
    app.backToHome(this);
  },
  unOpen: function() {
    app.unOpen();
  },
  getUserObj: function(e) {
    console.log(e);
    let errMsg = e.detail.errMsg;
    if (errMsg == 'getUserInfo:ok') {
      this.setData({
        userInfo: e.detail.userInfo
      })
    }
  },
  //复制客服微信号
  copyText: function(e) {
    util.copyText(e);
  },
  //跳转优惠券页
  toDiscountCardPage: function() {
    wx.navigateTo({
      url: '/pages/user/pages/discountCard/discountCard',
    })
  },
  getFormId: function(e) {
    // console.log(e);
    app.getFormId(e);
  },
})